package com.zqll.gdx.starter.assets;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.zqll.gdx.starter.assets.utils.TextUtils;

/**
 * 简单的中文支持示例
 * 展示如何使用gdx-chinese库来显示中文文本
 */
public class SimpleChineseExample extends ApplicationAdapter {

    public static void main(String[] args) {
        new Lwjgl3Application(new SimpleChineseExample());
    }

    private SpriteBatch batch;
    private Stage stage;
    private Skin skin;
    private BitmapFont incrementalFont;
    
    @Override
    public void create() {
        batch = new SpriteBatch();
        stage = new Stage(new ScreenViewport());
        
        // 初始化StarterAssets
        StarterAssets.init();
        
        // 创建Skin并添加中文字体
        skin = StarterAssets.getSkin();
        BitmapFont chineseFont = StarterAssets.font(16);
        skin.add("chinese-font", chineseFont);
        
        // 为特定文本生成增量字体
        String incrementalText = "这是增量字体测试：性能优化！";
        BitmapFont incrementalFont = StarterAssets.getFontGenerator().generateIncrementalFont(20, incrementalText, Color.GREEN);
        skin.add("incremental-font", incrementalFont);
        
        // 创建UI组件
        createUI();
        
        Gdx.input.setInputProcessor(stage);
    }
    
    private void createUI() {
        Table table = new Table();
        table.setFillParent(true);
        
        // 创建标签样式
        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font = skin.get("chinese-font", BitmapFont.class);
        labelStyle.fontColor = Color.WHITE;
        
        Label.LabelStyle incrementalStyle = new Label.LabelStyle();
        incrementalStyle.font = skin.get("incremental-font", BitmapFont.class);
        incrementalStyle.fontColor = Color.CYAN;
        
        // 创建按钮样式
        TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
        buttonStyle.font = skin.get("chinese-font", BitmapFont.class);
        buttonStyle.fontColor = Color.WHITE;
        buttonStyle.downFontColor = Color.GRAY;
        
        // 添加UI组件
        Label titleLabel = new Label("LibGDX 中文支持库示例", labelStyle);
        Label descLabel = new Label("这个示例展示了如何在LibGDX中显示中文文本", labelStyle);
        Label incrementalLabel = new Label("这是增量字体测试文本，只包含必要的字符。", incrementalStyle);
        
        TextButton button1 = new TextButton("中文按钮", buttonStyle);
        TextButton button2 = new TextButton("另一个按钮", buttonStyle);
        
        table.add(titleLabel).pad(20).row();
        table.add(descLabel).pad(10).row();
        table.add(incrementalLabel).pad(10).row();
        table.add(button1).pad(10).row();
        table.add(button2).pad(10).row();
        
        stage.addActor(table);
    }
    
    @Override
    public void render() {
        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        
        stage.act();
        stage.draw();
        
        // 直接使用TextUtils进行文本渲染
        batch.begin();
        
        // 获取增量字体进行直接渲染
        if (incrementalFont == null) {
            String directText = "直接渲染的中文文本：性能优化！";
            incrementalFont = StarterAssets.getFontGenerator().generateIncrementalFont(18, directText, Color.YELLOW);
        }
        
        // 使用TextUtils进行文本测量和渲染
        String renderText = "直接渲染的中文文本：性能优化！";
        float textWidth = TextUtils.getTextWidth(incrementalFont, renderText);
        float textHeight = TextUtils.getTextHeight(incrementalFont, renderText);
        
        float x = (Gdx.graphics.getWidth() - textWidth) / 2;
        float y = 100;
        
        TextUtils.drawText(batch, incrementalFont, renderText, x, y, Color.YELLOW);
        
        // 显示文本信息
        BitmapFont infoFont = skin.get("chinese-font", BitmapFont.class);
        String info = String.format("文本尺寸: %.1f x %.1f", textWidth, textHeight);
        TextUtils.drawText(batch, infoFont, info, 10, 50, Color.WHITE);
        
        // 显示字符统计信息
        // Gdx.app.log("SimpleChineseExample", "字体生成器已初始化");
        
        batch.end();
    }
    
    @Override
    public void dispose() {
        batch.dispose();
        stage.dispose();
        StarterAssets.dispose();
    }
}